package com.corpgovernment.organization.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//import com.fasterxml.jackson.core.JsonParser.Feature;

/**
 * <p>Json工具类，主要是针对springMVC框架</p>
 * <p>依赖com.fasterxml.jackson</p>
 * @Author Hubal 2016年11月18日
 */
public class JsonUtils {

    // 定义jackson对象
    private static ObjectMapper MAPPER = new ObjectMapper();
    static{
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	MAPPER.setDateFormat(format);
    }
    /**
     * @Description: 将对象转换成json字符串
     * @param Object
     * @return String
     */
    public static String object2Json(Object data) {
    	if(data == null){
    		return "";
    	}
    	try {
			String string = MAPPER.writeValueAsString(data);
			return string;
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
    	return "";
    }
    /**
     * @Describe: json字符串转为Map
     * @author: Hubal in 2018-01-29
     */
	public static Map<String, Object> json2Map(String json) {
		Map<String, Object> map = new HashMap<String, Object>(); 
		try {
			map = MAPPER.readValue(json, new TypeReference<HashMap<String,Object>>(){});
		} catch (Exception e) {
			e.printStackTrace();
		}  
		/*Map<String, Object> data = new HashMap<String, Object>();
		JSONObject jsonObject = JSONObject.fromObject(json);
		Iterator it = jsonObject.keys();
		while (it.hasNext()) {
			String key = String.valueOf(it.next());
			String value = (String) jsonObject.get(key);
			data.put(key, value);
		}*/
		return map;
	}
    
    /**
     * 将json结果集转化为对象
     * @param jsonData json数据
     * @param clazz 对象中的object类型
     * @return
     */
    public static <T> T json2Pojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将json数据转换成pojo对象list
     * <p>Title: jsonToList</p>
     * <p>Description: </p>
     * @param jsonData
     * @param beanType
     * @return 
     */
    public static <T>List<T> json2List(String jsonData, Class<T> beanType) {
    	JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
    	try {
    		List<T> list = MAPPER.readValue(jsonData, javaType);
    		return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return null;
    }
    /**
     * <p>组装jsons为List<Map<String,Object>></p>
     * @Author Hubal 2017年2月9日
     * @param jsonString
     * @return List<Map<String,Object>>
     */
    public static List<Map<String,Object>> json2ListMap(String jsonString){
    	try {
    		
    		List<Map<String,Object>> list = MAPPER.readValue(jsonString,new TypeReference<List<Map<String,Object>>>() { });
//    		JavaType javaType =MAPPER.getTypeFactory().constructParametricType(List.class,HashMap.class); 
//    		List<Map<String,Object>> list = MAPPER.readValue(jsonString, javaType);
    		return list;
    	} catch (Exception e) {
			e.printStackTrace();
		}
		return null;  
    }
}
